<?php
/**
 * Fang_DAO_Message
 * 
 * 作者:  姚刚 (yaogang@the9sense.com)
 * 创建时间: 2011-04-13 22:08:07
 * 修改记录: 
 * 
 * $Id: Message.php 20 2011-04-17 13:00:27Z coosuu $
 */

class Fang_DAO_Message extends DAO_Mysql {

    const STATUS_UNPASS = 1;

    const STATUS_PASS = 2;

    const STATUS_REMOVE = 9;

    private static $_table = 'fang_messages';

    /**
     * listByStatus 
     * 
     * @param  mixed $status 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public static function listByStatus($status, $pageOptions = array(), $fields = array()) {

        $conditions = 'me_status = ? AND me_parent_id = ? AND me_status != ?';
        $params = array($status, 0, self::STATUS_REMOVE);
        return parent::getAll(self::$_table, $conditions, $params, $fields, $pageOptions, array('meid' => 'DESC'));
    }

    /**
     * countByStatus 
     * 
     * @param  mixed $status 
     * @return void
     */
    public static function countByStatus($status) {

        $conditions = 'me_status = ? AND me_parent_id = ? AND me_status != ?';
        $params = array($status, 0, self::STATUS_REMOVE);
        return parent::count(self::$_table, $conditions, $params);
    }

    /**
     * listPass 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public static function listPass($pageOptions = array(), $fields = array()) {

        return self::listByStatus(self::STATUS_PASS, $pageOptions, $fields);
    }

    /**
     * countPass 
     * 
     * @return void
     */
    public static function countPass() {

        return self::countByStatus(self::STATUS_PASS);
    }

    /**
     * listUnpass 
     * 
     * @param  array $pageOptions 
     * @param  array $fields 
     * @return void
     */
    public static function listUnpass($pageOptions = array(), $fields = array()) {

        return self::listByStatus(self::STATUS_UNPASS, $pageOptions, $fields);
    }

    /**
     * countUnpass 
     * 
     * @return void
     */
    public static function countUnpass() {

        return self::countByStatus(self::STATUS_UNPASS);
    }

    /**
     * listRepliesByMeids 
     * 
     * @param  mixed $meids 
     * @param  array $fields 
     * @return void
     */
    public static function listRepliesByMeids($meids, $fields = array()) {

        $params = array();
        $conditions = 'me_parent_id IN (' . parent::buildInSQLClause($meids, $params) . ') AND me_status != ?';
        $params[] = self::STATUS_REMOVE;
        return parent::getAll(self::$_table, $conditions, $params, $fields);
    }

    /**
     * add 
     * 
     * @param  mixed $data 
     * @return void
     */
    public static function add($data) {

        if (!$data['me_status']) {
            $data['me_status'] = self::STATUS_UNPASS;
        }

        if (!$data['me_created']) {
            $data['me_created'] = date('Y-m-d H:i:s');
        }

        return parent::add(self::$_table, $data);
    }

    /**
     * update 
     * 
     * @param  mixed $meid 
     * @param  mixed $data 
     * @return void
     */
    public static function update($meid, $data) {

        if (!$data['me_updated']) {
            $data['me_updated'] = date('Y-m-d H:i:s');
        }

        return parent::updateByKey(self::$_table, $data, $meid);
    }

    /**
     * remove 
     * 
     * @param  mixed $meid 
     * @return void
     */
    public static function remove($meid) {

        if (!$data['me_deleted']) {
            $data['me_deleted'] = date('Y-m-d H:i:s');
        }

        if (!$data['me_status']) {
            $data['me_status'] = self::STATUS_REMOVE;
        }

        return self::update($meid, $data);
    }
}
